#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "qcustomplot.h"
#include "dataclass.h"
#include <QFileDialog>
#include <QDebug>


DataClass *dataC = new DataClass;
QVector<double> xx(7882);
QVector<double> ymax0(7882), ymax1(7882), ymax2(7882), ymax3(7882), ymax4(7882), ymax5(7882);
QVector<double> ymax6(7882), ymax7(7882), ymax8(7882), ymax9(7882), ymaxA(7882), ymaxB(7882);


QVector<double> ymin(7882);
float minX=0,maxX,minY=0,maxY=4096;


MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    QCustomPlot *customPlot = ui->widget;//customPlot指针指向控件

    connect(ui->actionWaveform, &QAction::triggered, this, &MainWindow::waveForm);
    connect(ui->actionHome, &QAction::triggered, this, &MainWindow::homeWaveForm);

    customPlot->setParent(this);

    customPlot->addGraph();
    customPlot->graph(0)->setPen(QPen(Qt::blue));

    customPlot->addGraph();
    customPlot->graph(1)->setPen(QPen(Qt::red));

    customPlot->addGraph();
    customPlot->graph(2)->setPen(QPen(Qt::yellow));

    customPlot->addGraph();
    customPlot->graph(3)->setPen(QPen(Qt::green));

    customPlot->addGraph();
    customPlot->graph(4)->setPen(QPen(Qt::black));

    customPlot->addGraph();
    customPlot->graph(5)->setPen(QPen(Qt::magenta));

    customPlot->addGraph();
    customPlot->graph(6)->setPen(QPen(Qt::lightGray));

    customPlot->addGraph();
    customPlot->graph(7)->setPen(QPen(Qt::darkYellow));

    customPlot->addGraph();
    customPlot->graph(8)->setPen(QPen(Qt::darkRed));

    customPlot->addGraph();
    customPlot->graph(9)->setPen(QPen(Qt::darkCyan));

    customPlot->addGraph();
    customPlot->graph(10)->setPen(QPen(Qt::cyan));

    customPlot->addGraph();
    customPlot->graph(11)->setPen(QPen(Qt::darkBlue));

    customPlot->xAxis2->setVisible(true);
    customPlot->xAxis2->setTickLabels(false);
    customPlot->yAxis2->setVisible(true);
    customPlot->yAxis2->setTickLabels(false);

    /*通过下面这句语句，可以屏蔽掉QCustomPlot的鼠标消息*/
    customPlot->setAttribute(Qt::WA_TransparentForMouseEvents, true);

}

void MainWindow::mD2QVector(){
    for (int i=0; i<7882; ++i){
        xx[i] = dataC->indData[i];
        ymax0[i] = dataC->maxData0[i];
        //ymin[i] = dataC->minData1[i];
        ymax1[i] = dataC->maxData1[i];
        ymax2[i] = dataC->maxData2[i];
        ymax3[i] = dataC->maxData3[i];
        ymax4[i] = dataC->maxData4[i];
        ymax5[i] = dataC->maxData5[i];
        ymax6[i] = dataC->maxData6[i];
        ymax7[i] = dataC->maxData7[i];
        ymax8[i] = dataC->maxData8[i];
        ymax9[i] = dataC->maxData9[i];
        ymaxA[i] = dataC->maxDataA[i];
        ymaxB[i] = dataC->maxDataB[i];
    }
    ui->widget->graph(0)->setData(xx, ymax0);
    ui->widget->graph(1)->setData(xx, ymax1);
    ui->widget->graph(2)->setData(xx, ymax2);
    ui->widget->graph(3)->setData(xx, ymax3);
    ui->widget->graph(4)->setData(xx, ymax4);
    ui->widget->graph(5)->setData(xx, ymax5);
    ui->widget->graph(6)->setData(xx, ymax6);
    ui->widget->graph(7)->setData(xx, ymax7);
    ui->widget->graph(8)->setData(xx, ymax8);
    ui->widget->graph(9)->setData(xx, ymax9);
    ui->widget->graph(10)->setData(xx, ymaxA);
    ui->widget->graph(11)->setData(xx, ymaxB);
}

void MainWindow::homeWaveForm(){
    maxX = dataC->index0;
    dataC->findMaxMinData(0, maxX);
    qDebug()<<"find max/min/ind over";

    mD2QVector();

    ui->widget->graph(0)->rescaleAxes();
    ui->widget->yAxis->setRange(0, 4096);
    ui->widget->replot();
}

void MainWindow::waveForm(){
    QString str = QFileDialog::getOpenFileName(this,"打开文件", "C:/Users/62320/Desktop", "(*.txt)");
    qDebug()<<str;
    dataC->txt2dataV(str.toUtf8().data());
    qDebug()<<"txt to dataVector over";
    homeWaveForm();
    QLabel * label = new QLabel("文件路径："+str,this);
    ui->statusBar->addWidget(label);
}

void MainWindow::resetRange(){
    float minmin;

    if(begin_x_val<end_x_val){
        minmin = begin_x_val;
    }else{
        minmin = end_x_val;
    }

    if(minmin<0){
        QMessageBox::warning(this, "warning", "警告:您刚刚框选了Y轴左侧区域");
    }else{
        if(begin_x_val<end_x_val){
            minX = begin_x_val;
            maxX = end_x_val;
        }else{
            minX = end_x_val;
            maxX = begin_x_val;
        }
        if(begin_y_val<end_y_val){
            minY = begin_y_val;
            maxY = end_y_val;
        }else{
            minY = end_y_val;
            maxY = begin_y_val;
        }

        dataC->findMaxMinData(minX,maxX);

        mD2QVector();

        ui->widget->xAxis->setRange(minX, maxX);
        ui->widget->yAxis->setRange(minY, maxY);
        ui->widget->replot();
    }
}

void MainWindow::resetPos(){
    //···
    float changeX,changeY;
    float minmin=minX;
    changeX = end_x_val-begin_x_val;
    changeY = end_y_val-begin_y_val;

    minmin -= changeX;

    if(minmin<0){
        //
        maxX -= minX;
        minX = 0;
        QMessageBox::warning(this, "warning", "警告：您已向右拖动到了极限");
    }else{
        minX -= changeX;
        maxX -= changeX;
    }
    minY -= changeY;
    maxY -= changeY;

    dataC->findMaxMinData(minX,maxX);

    mD2QVector();

    ui->widget->xAxis->setRange(minX, maxX);
    ui->widget->yAxis->setRange(minY, maxY);
    ui->widget->replot();
}

void MainWindow::mousePressEvent(QMouseEvent *event){
    int x_pos = event->pos().x();
    int y_pos = event->pos().y();

    // 把鼠标坐标点 转换为 QCustomPlot 内部坐标值 （pixelToCoord 函数）
    // coordToPixel 函数与之相反 是把内部坐标值 转换为外部坐标点
    begin_x_val = ui->widget->xAxis->pixelToCoord(x_pos);
    begin_y_val = ui->widget->yAxis->pixelToCoord(y_pos);
    //这里在读取y轴坐标的时候会有误差，做一个补偿
    //begin_y_val += 120;
    qDebug()<<"BWX="<<x_pos<<";BWY="<<y_pos<<"***BPX="<<begin_x_val<<";BPY="<<begin_y_val;

}

void MainWindow::mouseReleaseEvent(QMouseEvent *event){
    int x_pos = event->pos().x();
    int y_pos = event->pos().y();

    // 把鼠标坐标点 转换为 QCustomPlot 内部坐标值 （pixelToCoord 函数）
    // coordToPixel 函数与之相反 是把内部坐标值 转换为外部坐标点
    end_x_val = ui->widget->xAxis->pixelToCoord(x_pos);
    end_y_val = ui->widget->yAxis->pixelToCoord(y_pos);
    //这里在读取y轴坐标的时候会有误差，做一个补偿
    //end_y_val += 120;
    qDebug()<<"EWX="<<x_pos<<";EWY="<<y_pos<<"***EPX="<<end_x_val<<";EPY="<<end_y_val;

    if(event->button() == Qt::RightButton){
        resetRange();
    }else if (event->button() == Qt::LeftButton) {
        resetPos();
    }
}

MainWindow::~MainWindow(){
    delete ui;
}



//目前出现的问题：当点击Qcustomplot下面的区域的时候，也会重新框选。
//拟采用的解决方案：将配置文件采用非模态对话框的方式弹出，然后进行配置

//目前出现的问题：当使用右键时，依旧会框选，并出现bug。
//现在的解决方案：当使用右键时，是框选功能
//当使用左键时，是拖动波形功能

//目前要求的功能：鼠标可以拖动波形
//拟采用的解决方案：鼠标左键可以上下左右拖动？？？？
//这里需要注意一个问题，如果是上下拖动，只需要更改range即可
//但是如果是左右拖动，需要重新选点！！！
//如果是可以同时上下左右拖动呢？？？？
